package com.guoyu.project.web.admin;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Banner;
import com.guoyu.project.model.Project_Manage;
import com.guoyu.project.service.FileUploadService;
import com.guoyu.project.service.Project_ManageService;

@Controller
@RequestMapping("/admin/PManage")
public class Project_ManageController {
	
	    //日志
	    private final static Logger logger = LoggerFactory.getLogger(Project_ManageController.class);
        
	    @Resource
	    private Project_ManageService project_ManageService;
	    
	    @Resource
	    private FileUploadService fileUploadService;
        
	    
	    @RequestMapping("index")
	    public String index() {
	        return "admin/PManage/index";
	    }


	    /**
	     * pm信息列表页
	     *
	     * @return
	     */

	    @RequestMapping("pmlist")
	    @ResponseBody
	    public Pagination<Project_Manage> list(@RequestParam(defaultValue = "0") int offset,
	            @RequestParam(defaultValue = "10") int limit) {	    
	    	Map<String, Object> params = Maps.newHashMap();
	        return project_ManageService.queryAllProject_Manage(offset, limit, params);
	    }

	   
	    /**
	     * 跳到修改信息页面
	     *
	     * @return
	     */
	    @RequestMapping("edit")
	    public String edit(Integer pId, Model model) {
	    	Project_Manage project_Manage = project_ManageService.queryById(pId);
	        model.addAttribute("project_Manage", project_Manage);
	        return "admin/PManage/edit";
	    }

	    /**
	     * 更新信息
	     *
	     * @return
	     */
	    @RequestMapping("update")
	    @ResponseBody
	    public APIResponse update(@RequestParam(value = "ProjectPic", required = false) MultipartFile picture,
	    		@RequestParam(value = "ProjectUpdateFil111", required = false)MultipartFile fiFile, 
	    		@RequestParam(value = "ProjectUpdateFil211", required = false)MultipartFile seFile, 
	    		@RequestParam(value = "ProjectUpdateFil311", required = false)MultipartFile thFile, 
	    		Project_Manage project_manage) {
	        logger.info("提交修改project_manage信息信息：{}", JsonUtil.encode(project_manage));
	        String picPath = StringUtils.defaultIfBlank(fileUploadService.upload(picture), project_manage.getPicture());
	        project_manage.setPicture(picPath);
	      //文件上传
	        String fifilePath=fileUploadService.upload(fiFile);
	        project_manage.setFiFile(fifilePath);
	        
	        String sefilePath=fileUploadService.upload(seFile);
	        project_manage.setSeFile(sefilePath);
	        
	        String thfilePath=fileUploadService.upload(thFile);
	        project_manage.setThFile(thfilePath);
	        
	        project_ManageService.updateProject_Manage(project_manage);
	        return APIResponse.returnSuccess();
	    }

	    /**
	     * 删除
	     * @return
	     */
	    @RequestMapping("delete")
	    @ResponseBody
	    public APIResponse delete(Integer pId) {
	    	project_ManageService.deleteById(pId);
	        return APIResponse.returnSuccess();
	    }

	}